//
//  HarvestViewController.m
//  BabyAndBabyDriver
//
//  Created by 范琦 on 16/8/10.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "HarvestViewController.h"
#import "HarvestTopView.h"
#import "HarvestOneViewController.h"
#import "HarvestTwoViewController.h"
#import "HarvestThreeViewController.h"
#import "HarvestAddViewController.h"

@interface HarvestViewController ()
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) HarvestTopView *topView;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation HarvestViewController


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;


}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    _mainView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_mainView];
    [self creatTopView];
    [self creatScrollView];
    [self creatThreeView];
    [self creatRight];

}
- (void)creatRight
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"添加" forState:UIControlStateNormal];
    btn.frame = CGRectMake(0, 0, 30, 20);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        HarvestAddViewController *add = [[HarvestAddViewController alloc]init];
        [self.navigationController pushViewController:add animated:YES];
    }];
    
}
- (void)creatTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"HatvestTopView" owner:nil options:nil] lastObject];
    _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, 40);
    [self.mainView addSubview:_topView];
    _topView.lineView.backgroundColor = BACKGROUNDCOLOR;
    
    [[_topView.twoBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        CGRect rect = _topView.lineView.frame;
        _topView.lineView.frame = CGRectMake(_topView.twoBtn.frame.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        [_scrollView setContentOffset:CGPointMake(KSCREENWIDTH, 0) animated:YES];
        
    }];
    [[_topView.oneBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        CGRect rect = _topView.lineView.frame;
        _topView.lineView.frame = CGRectMake(_topView.oneBtn.frame.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
        
    }];
    
    [[_topView.threeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        CGRect rect = _topView.lineView.frame;
        _topView.lineView.frame = CGRectMake(_topView.threeBtn.frame.origin.x, rect.origin.y, rect.size.width, rect.size.height);
        [_scrollView setContentOffset:CGPointMake(KSCREENWIDTH * 2, 0) animated:YES];
        
        
    }];
}
- (void)creatScrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, KSCREENWIDTH, KSCREENHEIGHT)];
    _scrollView.contentSize = CGSizeMake(KSCREENWIDTH * 3, 0);
    _scrollView.pagingEnabled = YES;
    [self.mainView addSubview:_scrollView];
    
    [RACObserve(_scrollView, contentOffset) subscribeNext:^(id x) {
        
        NSValue *toValue = (NSValue *)x;
        CGPoint c;
        [toValue getValue:&c];
        
        NSInteger i = c.x / KSCREENWIDTH;
        CGRect rect = _topView.lineView.frame;
        
        _topView.lineView.frame = CGRectMake(i * _topView.oneBtn.bounds.size.width, rect.origin.y, rect.size.width, rect.size.height);
    }];
}
- (void)creatThreeView
{
    HarvestOneViewController *oneVC = [[HarvestOneViewController alloc]init];
    [self addChildViewController:oneVC];
    oneVC.view.frame = CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 - 40 - self.tabBarController.tabBar.frame.size.height);
    [self.scrollView addSubview:oneVC.view];
    
    
    HarvestTwoViewController *twoVC = [[HarvestTwoViewController alloc]init];
    [self addChildViewController:twoVC];
    twoVC.view.frame = CGRectMake(KSCREENWIDTH, 0, KSCREENWIDTH,KSCREENHEIGHT - 64 - 40 - self.tabBarController.tabBar.frame.size.height);
    [self.scrollView addSubview:twoVC.view];
    
    
    HarvestThreeViewController *threeVC = [[HarvestThreeViewController alloc]init];
    [self addChildViewController:threeVC];
    threeVC.view.frame = CGRectMake(KSCREENWIDTH * 2, 0, KSCREENWIDTH, KSCREENHEIGHT - 64 - 40 - self.tabBarController.tabBar.frame.size.height);
    [self.scrollView addSubview:threeVC.view];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
